<?php

require_once 'ActionFramework/start.php';

class ManageBlockAction extends Action {
	
	const INPUT_BLOCKED_USER_ID = "blocked_user_id";
	const INPUT_OPERATION = "operation";
	
	const BLOCK = 'block';
	const UNBLOCK = 'unblock';
	
	public function __construct() {
		parent::__construct();
		$this->add_input(new Input(ManageBlockAction::INPUT_BLOCKED_USER_ID));
		$this->add_input(new MultiChoiceInput(
			ManageBlockAction::INPUT_OPERATION, 
			null, 
			array(
				// Use action as id for action itself 
				ManageBlockAction::BLOCK => ManageBlockAction::BLOCK,
				ManageBlockAction::UNBLOCK => ManageBlockAction::UNBLOCK
			)
		));
	}
	
	public function trigger($request) {
		
		$operation = $request[ManageBlockAction::INPUT_OPERATION];
		$blocked_user_id = intval($request[ManageBlockAction::INPUT_BLOCKED_USER_ID]);
		
		global $DB;
		$user = $DB->get_user_by_id($blocked_user_id);
		
		if ($operation == ManageBlockAction::BLOCK) {
			$user->blocked = true;
			$message = 'The user is now blocked and can no longer do anything on this website.';
		}
		else {
			$user->blocked = false;
			$message = 'The user is no longer blocked.';
		}
		$DB->save_user($user);
		
		$view = r(new AdminAction())->trigger($request);
		$view->add_message($message);
		return $view;
		
	}
	
}

?>
